package com.barunlab.ems.client.control;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.apache.log4j.Logger;

import com.barunlab.ems.constants.EmsRequestType;
import com.barunlab.ems.model.EmsRequest;
import com.barunlab.ems.model.EmsResponse;
import com.barunlab.ems.model.EmsResult;
import com.barunlab.ems.model.UserInfo;
import com.barunlab.ems.rmi.ServerInterface;

public class ClientManager {
	private static Logger LOGGER = Logger.getLogger(ClientManager.class);
	
	private static ServerInterface smsInterface;
	
	static {
		try {
			new ClientManager();
		}
		catch (Exception ex) {
			LOGGER.error("ClientManager 생성 실패", ex);
			System.exit(1);
		}
	}
	
	private ClientManager() throws Exception {
		initEventWorker();
		initRmi();
	}
	
	private void initEventWorker() throws Exception {
		
	}
	
	private void initRmi() throws Exception {
		Registry registry = LocateRegistry.getRegistry("localhost", 5001);
		
		smsInterface = (ServerInterface)registry.lookup("SMS");
	}
	
	public static EmsResult login(String userId, String password) throws Exception {
		UserInfo userInfo = new UserInfo();
		userInfo.setId(userId);
		userInfo.setPassword(password);
		
		Object[] params = new Object[1];
		params[0] = userInfo;
		
		EmsRequest request = new EmsRequest(null, EmsRequestType.LOGIN, params);
		
		EmsResponse response = smsInterface.command(request);
		return response.getResult();
	}
	
	public static EmsResult getUserInfo(String userId) throws Exception {
		UserInfo userInfo = new UserInfo();
		userInfo.setId(userId);
		
		Object[] params = new Object[1];
		params[0] = userInfo;
		
		EmsRequest request = new EmsRequest(null, EmsRequestType.LIST_USER, params);
		
		EmsResponse response = smsInterface.command(request);
		return response.getResult();
	}
}
